package generator.statements;

import generator.addons.SimpleDataGenerator;
import generator.collections.TravellerTable;
import java.util.Random;

/**
 * Traveller SQL record mapper.
 * 
 * @author Necro
 * @author Marek Snopkowski <snpy@snpy.info>
 * @version 12.04.2008
 */
public class Traveller extends Statement 
{
    private int travellerID;
    private String name;
    private String phone;
    private String documentID;

    public Traveller(String name, String phone, String nip) 
    {        
        this.travellerID = TravellerTable.autoincrement();
        this.name        = name;
        this.phone       = phone;
        Random r = new Random();
        this.documentID  = null == nip || nip.equalsIgnoreCase("NULL")
                ? String.format("A%03d-%03d-%02d-%02d", 
                    r.nextInt(999), r.nextInt(999), r.nextInt(99), r.nextInt(99))
                : "A" + nip;
    }
    
    public Traveller(Customer customer)
    {
        
        this(customer.getName(), customer.getPhone(), customer.getNip());
    }
    
    /**
     * Generuje nowego podroznika lub zwraca juz istniejacego z tabeli TravellerTable.
     * 
     * @param existingCustomer Klient, ktory moze sie stac zarazem uzytkownikiem.
     * @return
     */
    public static Traveller getRand(Customer existingCustomer)
    {
        return ((new Random()).nextInt(10) > 8 && hasSufficientTravellers()) 
                ? getExistingTraveller()
                : new Traveller(existingCustomer);
    }
    
    public static Traveller getRand()
    {
        return ((new Random()).nextInt(10) > 8 && hasSufficientTravellers()) 
                ? getExistingTraveller()
                : new Traveller(SimpleDataGenerator.getName(),
                    SimpleDataGenerator.getPhone(), 
                    SimpleDataGenerator.getNIP());
    }
    
    public static boolean hasSufficientTravellers()
    {
        return TravellerTable.getInstance().count() > 10;
    }
    
    public static Traveller getExistingTraveller()
    {
        //zmieniam na customerTable
        return TravellerTable.getInstance().selectRandom();
    }
   
    public Object[] getFieldsValueCollection()
    {
        return new Object[]{ travellerID, name, phone, documentID };
    }
    
    public String getFieldsNameSet()
    {
        return "[traveller_id], [name], [phone], [document_id]";
    }

    @Override
    public String getTableName()
    {
        return "traveller";
    }
}
